package com.resjz.milddog.controller;

import com.alibaba.fastjson.JSONObject;
import com.purgeteam.cloud.dispose.starter.Result;
import com.resjz.milddog.service.UserLoginService;
import org.apache.oltu.oauth2.client.OAuthClient;
import org.apache.oltu.oauth2.client.URLConnectionClient;
import org.apache.oltu.oauth2.client.request.OAuthClientRequest;
import org.apache.oltu.oauth2.common.OAuth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.loadbalancer.CompletionContext;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.resjz.milddog.utils.AuthToken;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/user")
public class UserLoginController {
    //客户端id
    @Value("${auth.clientId}")
    private String clientId;
    //客户端密钥
    @Value("${auth.clientSecret}")
    private String clientSecret;
    @Autowired
    private UserLoginService userLoginService;
    @RequestMapping("/login")
    //@ResponseBody
    public Result login(@RequestParam(required = false,value = "username",defaultValue = "")String username,@RequestParam(required = false,value = "password",defaultValue = "") String password, HttpServletResponse response) throws Exception {
        //授权方式 密码授权方式
        String grant_type = "password";
        System.out.println("1111111111111111111");
        JSONObject token = userLoginService.login(username, password, clientId, clientSecret, grant_type);
        System.out.println("token::::::::::::::::"+token.toString());
        return Result.ofSuccess(token);

    }

    @RequestMapping("/toLogin")
    public String toLogin(@RequestParam(required = false,value = "From",defaultValue = "") String from, Model model){
        from.replace("%2F","/");
        model.addAttribute("from",from.toString());
        return "login";
    }

   /* @RequestMapping(value = "doLogin")
    public JSONObject deviceInfo(String username, String password, String client_id, String response_type) {
    }*/
}
